package Leo.redis;

import ch.qos.logback.core.util.CloseUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class SingleRedis {

    public static void main(String[] args) {
        SingleRedis redis = new SingleRedis();
        redis.singleEntity();
        System.out.println("@@@@@@@@@@@@@@");
        redis.reidsPool();
    }

    public void singleEntity(){
        Jedis jedis = new Jedis("192.168.80.251",6379);
        jedis.auth("southmax");
        jedis.set("team","LGD");
        List<Object> member = new ArrayList();
        member.add("YYF");
        member.add("430");
        member.add("xiao8");
        jedis.set("member".getBytes(),ObjectsTranscoder.serialize(member));
        System.out.println(jedis.get("team"));
        byte[] bytes = jedis.get("member".getBytes());
        List<Object> deserialize = ObjectsTranscoder.deserialize(bytes);
        deserialize.forEach(o ->{
            if(o instanceof String){
                System.out.println((String) o);
            }});
        jedis.close();
    }

    public void reidsPool(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(10);
        config.setMaxIdle(5);

        JedisPool jedisPool = new JedisPool(config, "192.168.80.251", 6379,10,"southmax");
        Jedis resource = jedisPool.getResource();
        System.out.println(resource.exists("team"));
        System.out.println(resource.exists("member"));
        System.out.println(resource.exists("member".getBytes()));
        resource.close();
    }
    static class ObjectsTranscoder{

        public static byte[] serialize(List<Object> value) {
            if (value == null) {
                throw new NullPointerException("Can't serialize null");
            }
            byte[] rv=null;
            ByteArrayOutputStream bos = null;
            ObjectOutputStream os = null;
            try {
                bos = new ByteArrayOutputStream();
                os = new ObjectOutputStream(bos);
                for(Object user : value){
                    os.writeObject(user);
                }
                os.writeObject(null);
                os.close();
                bos.close();
                rv = bos.toByteArray();
            } catch (IOException e) {
                throw new IllegalArgumentException("Non-serializable object", e);
            } finally {
                try {
                    os.close();
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return rv;
        }

        public static List<Object> deserialize(byte[] in) {
            List<Object> list = new ArrayList<Object>();
            ByteArrayInputStream bis = null;
            ObjectInputStream is = null;
            try {
                if(in != null) {
                    bis=new ByteArrayInputStream(in);
                    is=new ObjectInputStream(bis);
                    while (true) {
                        Object user = (Object) is.readObject();
                        if(user == null){
                            break;
                        }else{
                            list.add(user);
                        }
                    }
                    is.close();
                    bis.close();
                }
            } catch (IOException e) {
                System.out.println("Caught IOException decoding %d bytes of data" +
                        in == null ? 0 : in.length);
            } catch (ClassNotFoundException e) {
                System.out.println("Caught CNFE decoding %d bytes of data" +
                        in == null ? 0 : in.length);
            } finally {
                try {
                    is.close();
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return list;
        }
    }

    static class ListTranscoder{
        public static byte[] serialize(Object value) {
            if (value == null) {
                throw new NullPointerException("Can't serialize null");
            }
            byte[] rv=null;
            ByteArrayOutputStream bos = null;
            ObjectOutputStream os = null;
            try {
                bos = new ByteArrayOutputStream();
                os = new ObjectOutputStream(bos);
                os.writeObject(value);
                os.close();
                bos.close();
                rv = bos.toByteArray();
            } catch (IOException e) {
                throw new IllegalArgumentException("Non-serializable object", e);
            } finally {
                try {
                    os.close();
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return rv;
        }

        public static Object deserialize(byte[] in) {
            Object rv=null;
            ByteArrayInputStream bis = null;
            ObjectInputStream is = null;
            try {
                if(in != null) {
                    bis=new ByteArrayInputStream(in);
                    is=new ObjectInputStream(bis);
                    rv=is.readObject();
                    is.close();
                    bis.close();
                }
            } catch (IOException e) {
                System.out.println("Caught IOException decoding %d bytes of data" +
                        in == null ? 0 : in.length);
            } catch (ClassNotFoundException e) {
                System.out.println("Caught CNFE decoding %d bytes of data" +
                        in == null ? 0 : in.length);
            } finally {
                try {
                    is.close();
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return rv;
        }
    }

}
